/* AVRlib Header files.*/
#include <avr/io.h>

#define DEBUG		

#define MAIN_nSCREEN        70

#include "buf_buffer.h"
#include "uart.h"


/* Local Header Files*/
#include "loc_types.h"

#include "loc_comlink.h"
#include "loc_scrnframe.h"
#include "loc_msgmanager.h"
#include "ses_manager.h"
#include "eff_manager.h"
#include "tim_control.h"
#include "srg_dirver.h"
#include "urt_driver.h"

#include <avr/pgmspace.h>

#define u8SCR_COUNTER_LIMIT     (u8)3



volatile static u8 u8SCR_TaskCounter;


#ifdef DEBUG

BUF_tstrBuffer * strUARTReceiveBuffer;
u16 u16UARTDataLength;
u8 u8UARTTempData;
u8 kau8LeddisString[] PROGMEM = "\r\nLEDDIS>";
#endif /* DEBUG */


int main(void)
{
    u8SCR_TaskCounter = TYP_u8ZERO;
    SRG_vidInit();
    MSG_vidInit();
    SCR_vidInit();
	EFF_vidInit();
    TIM_vidInit();
	URT_vidInit();

#ifdef DEBUG
strUARTReceiveBuffer = URT_strGetReceiveBuffer();
u16UARTDataLength = 0;
u8UARTTempData = 0;
URT_bSendString((u8 *)kau8LeddisString);
#endif /* DEBUG */

    while (TYP_u8TRUE)
    {
        if (TIM_bBackgroundFlag == TYP_u8TRUE)
        {
			
            TIM_bBackgroundFlag = TYP_u8FALSE;
        
            u8SCR_TaskCounter++;
            if (u8SCR_TaskCounter == u8SCR_COUNTER_LIMIT)
            {
                u8SCR_TaskCounter = TYP_u8ZERO;
				EFF_vidMainFunction();
            }      
        }
    
	#ifdef DEBUG

	u16UARTDataLength = BUF_u16GetDataLength(strUARTReceiveBuffer);

	if ( u16UARTDataLength != TYP_u16ZERO)
	{
		u8UARTTempData = BUF_u8GetFromFront(strUARTReceiveBuffer);
		URT_bSendByte(u8UARTTempData);
	
	}

	#endif /* DEBUG */
	}




}








/*  ________*/
/*  |       \*/
/*  |        \*/
/*  |        |*/
/*  |        |*/
/*  |        |*/
/*  |        |*/
/*  |        |*/
/*  |        |*/
/*  |        |*/
/*  |        |*/
/*  |        |*/
/*  |       /*/
/*  |______/*/

#if 0
u8 u8Debug = 2;
u8 test[] = "1234567890123456";
u8 x;
int main(void)
{
	u8 	u8TestVar;
	u8 	bufferIndex;
	u16 u16ScrCounter = 500;
	u8 testarray[30];
	u8 test3[] = "es";
	BUF_tstrBuffer testBuffer;
    
	SRG_vidInit();
    TIM_vidInit();
    BUF_vidInit(&testBuffer,testarray, 30 );
    COM_vidSetReceiveBuf(&testBuffer);
    SES_vidSetDataBuffer(&testBuffer);

	if (u8Debug == 1)
	{
	
	BUF_vidInit(&testBuffer,testarray,4 );
	uartInit();
	uartSetBaudRate(19200);
//	recvBuffer = uartGetRxBuffer();
    bufferIndex = 0;
    }
    /*
    for (i = 0; i< 6; i++)
    {
	bufferIndex = pgm_read_byte(&test[i]);
    bufferIndex = i;
    }
    */
    BUF_u8AddToEnd(&testBuffer,'x');
    BUF_u8AddToEnd(&testBuffer,'a');
	BUF_vidDumpFromFront(&testBuffer, 2);
	BUF_u8AddToEnd(&testBuffer,'t');
    BUF_u8AddToEnd(&testBuffer,'e');
	BUF_u8AddToEnd(&testBuffer,'s');
	BUF_u8AddToEnd(&testBuffer,'t');
    BUF_u8AddToEnd(&testBuffer,'t');
    BUF_u8AddToEnd(&testBuffer,'e');
	BUF_u8AddToEnd(&testBuffer,'s');
	BUF_u8AddToEnd(&testBuffer,'t');
	SCR_vidInit();
    MSG_vidInit();
	while(1)
	{
	if (u8Debug == 1)
	{
	bufferIndex = BUF_u8SearchString(&testBuffer,0,test3,2 );
	//	bufferIndex = 0;
    //bufferIndex = MSG_bAddToMsgBuff(test,16);
    x = MSG_u16GetReadyChar();
    bufferIndex = pgm_read_byte(&MSG_u8LUT[x]);
	//MSG_vidStartSavingMessages();
   // MSG_vidMainFunction();

    SCR_vidMainFunction();
	bufferIndex= SCR_u8GetReadyByte();
	//com_FrameUnpack(recvBuffer);
	}

    else if (u8Debug == 2)
    {
        SES_vidMainFunction();
        //BUF_u8AddToEnd(&testBuffer, bufferIndex);
       // bufferIndex = COM_u8ParseReceivedFrame();
    
    }
	else 
	{
	
	    u8TestVar = SCR_u8GetReadyByte();

	    if (u16ScrCounter == (u16)500)
	    {
	        u16ScrCounter = (u16)0;
	        SCR_vidMainFunction();
	    }
	    u16ScrCounter++;
	}
		
	}

}
#endif
